//
//  KAHomeOverdueView.swift
//  kano
//
//  Created by Frank on 2024/10/10.
//

import UIKit

class KAHomeOverdueView: UIView {
    
    var repayHandler:(()->())?
        
    lazy var var_bgView: UIImageView = {
        let var_bgView = UIImageView(image: UIImage(named: "billList_bg_white")) 
        var_bgView.isUserInteractionEnabled = true
        return var_bgView
    }()
    
    public var balanceTitle: UILabel = {
        let label = UILabel()
        label.numberOfLines = 1
        label.font = UIFont.systemFont(ofSize: kscale(12), weight: .medium)
        label.textColor = .color8A8891
        label.text = "Repayment balance"
        return label
    }()
    
//    public var balanceLabel: UILabel = {
//        let label = UILabel()
//        label.numberOfLines = 1
//        label.font = UIFont.systemFont(ofSize: kscale(12), weight: .medium)
//        label.textColor = .color323135
//        label.text = ""
//        return label
//    }()
    
    public var monthPayLabel: UILabel = {
        let label = UILabel()
        label.numberOfLines = 1
        label.font = UIFont.systemFont(ofSize: kscale(22), weight: .bold)
        label.textColor = .color323135
        label.text = ""
        return label
    }()
    
//    public var amountTitle: UILabel = {
//        let label = UILabel()
//        label.numberOfLines = 1
//        label.font = UIFont.systemFont(ofSize: kscale(12), weight: .medium)
//        label.textColor = .color8A8891
//        label.text = "Repayment amount"
//        return label
//    }()
//    
//    public var amountLabel: UILabel = {
//        let label = UILabel()
//        label.numberOfLines = 1
//        label.font = UIFont.systemFont(ofSize: kscale(12), weight: .medium)
//        label.textColor = .color323135
//        label.text = ""
//        return label
//    }()
    
    lazy var var_repayBtn: UIButton = {
        let var_repayBtn = UIButton()
        var_repayBtn.backgroundColor = .white
        var_repayBtn.addTarget(self, action: #selector(ka_repayBtnClick), for: .touchUpInside)
        var_repayBtn.setTitle("Repay now", for: .normal)
        var_repayBtn.setTitleColor(.themeColor, for: .normal)
        var_repayBtn.titleLabel?.font = UIFont.systemFont(ofSize: kscale(12), weight: .bold)
        var_repayBtn.layer.borderWidth = 1
        var_repayBtn.layer.borderColor = UIColor.themeColor.cgColor
        var_repayBtn.layer.cornerRadius = 8
        var_repayBtn.layer.masksToBounds = true
        return var_repayBtn
    }()
    
    @objc func ka_repayBtnClick() {
        if (repayHandler != nil) {
            repayHandler!()
        }
    }

    public var dueDateTitle: UILabel = {
        let label = UILabel()
        label.numberOfLines = 1
        label.font = UIFont.systemFont(ofSize: kscale(11), weight: .medium)
        label.textColor = .color615F67
        label.text = "Due Date"
        return label
    }()
    
    public var dueDateLabel: UILabel = {
        let label = UILabel()
        label.numberOfLines = 1
        label.font = UIFont.systemFont(ofSize: kscale(11), weight: .medium)
        label.textColor = .colorFF8921
        label.text = ""
        return label
    }()
    
    lazy var var_tipLabel: KAEdgeLabel = {
        let label = KAEdgeLabel(edgeInsets: UIEdgeInsets(top: 0, left: 6, bottom: 0, right: 6))
        label.numberOfLines = 1
        label.font = UIFont.systemFont(ofSize: kscale(9), weight: .medium)
        label.textColor = .colorFF8921
        label.text = "Due Today"
        label.backgroundColor = .colorFFE9D5
        label.layer.cornerRadius = 10
        label.layer.masksToBounds = true
        return label
    }()
    
    var model: KALoanBillList? {
        didSet {
            monthPayLabel.text = "₦\(model?.registrationConfirmation ?? 0)"
            
            dueDateLabel.text = Date.ka_stringWith(timestamp: model!.dueDateTime, format: "dd/MM/yyyy")
       
            if model!.overdueDuration >= -3 &&  model!.overdueDuration < -1 {
                var_tipLabel.isHidden = false
                var_tipLabel.text = "due after \(abs(model!.overdueDuration)) day"
                var_tipLabel.textColor = .colorFF8921
                var_tipLabel.backgroundColor = .colorFFE9D5
                dueDateLabel.textColor = .colorFF8921
            }
            else if model!.overdueDuration == -1 {
                var_tipLabel.isHidden = false
                var_tipLabel.text = "due Tomorrow"
                var_tipLabel.textColor = .colorFF8921
                var_tipLabel.backgroundColor = .colorFFE9D5
                dueDateLabel.textColor = .colorFF8921
            }
            else if model!.overdueDuration == 0 {
                var_tipLabel.isHidden = false
                var_tipLabel.text = "due Today"
                var_tipLabel.textColor = .colorFF8921
                var_tipLabel.backgroundColor = .colorFFE9D5
                dueDateLabel.textColor = .colorFF8921
            }
            else if model!.overdueDuration > 0 {
                var_tipLabel.isHidden = false
                var_tipLabel.text = "overdue \(abs(model!.overdueDuration)) day"
                var_tipLabel.textColor = .colorFF3644
                var_tipLabel.backgroundColor = .colorFFE8E8
                dueDateLabel.textColor = .colorFF3644
            } else {
                var_tipLabel.isHidden = true
                var_tipLabel.textColor = .themeColor
                dueDateLabel.textColor = .themeColor
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        ka_setUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}


extension KAHomeOverdueView {
    func ka_setUI() {
        backgroundColor = .white
        addSubview(var_bgView)
        var_bgView.addSubview(balanceTitle)
//        var_bgView.addSubview(balanceLabel)
        var_bgView.addSubview(monthPayLabel)
//        var_bgView.addSubview(amountTitle)
//        var_bgView.addSubview(amountLabel)
        var_bgView.addSubview(var_repayBtn)
        var_bgView.addSubview(dueDateTitle)
        var_bgView.addSubview(dueDateLabel)
        var_bgView.addSubview(var_tipLabel)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        var_bgView.snp.makeConstraints { make in
            make.left.equalTo(kscale(0));
            make.right.equalTo(kscale(0));
            make.bottom.equalTo(-0.5);
            make.top.equalTo(0.5);
        }
        balanceTitle.snp.makeConstraints { make in
            make.top.equalTo(kscale(26))
            make.left.equalTo(kscale(16))
        }
//        balanceLabel.snp.makeConstraints { make in
//            make.centerY.equalTo(balanceTitle)
//            make.left.equalTo(balanceTitle.snp.right).offset(kscale(8))
//        }
        
        monthPayLabel.snp.makeConstraints { make in
            make.left.equalTo(kscale(16))
            make.top.equalTo(balanceTitle.snp.bottom).offset(kscale(2))
        }
        
//        amountTitle.snp.makeConstraints { make in
//            make.left.equalTo(kscale(16))
//            make.top.equalTo(monthPayLabel.snp.bottom).offset(kscale(4))
//        }
//        amountLabel.snp.makeConstraints { make in
//            make.centerY.equalTo(amountTitle)
//            make.left.equalTo(amountTitle.snp.right).offset(kscale(8))
//        }
        var_repayBtn.snp.makeConstraints { make in
            make.size.equalTo(CGSizeMake(83, 30))
            make.right.bottom.equalTo(var_bgView).offset(kscale(-12));
        }
        dueDateTitle.snp.makeConstraints { make in
            make.left.equalTo(kscale(16))
            make.top.equalTo(var_repayBtn.snp.top).offset(0.5)
        }
        dueDateLabel.snp.makeConstraints { make in
            make.left.equalTo(dueDateTitle.snp.right).offset(kscale(8))
            make.top.equalTo(dueDateTitle)
        }
        
        var_tipLabel.snp.makeConstraints { make in
            make.centerY.equalTo(dueDateTitle)
            make.left.equalTo(dueDateLabel.snp.right).offset(kscale(8))
            make.height.equalTo(kscale(20))
        }
    }
    
    
}
